package com.markhsiu.MiniMQ.model;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;

import com.markhsiu.MiniMQ.model.constant.MessageSourceEnum;

/**
 * 远程传输消息体
 * @author Mark Hsiu
 *
 */
public abstract class Message implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String messageID;//消息ID
	private MessageSourceEnum source;//消息来源
	private MessageSourceEnum target;//消息目标
	private String topic ;//消息主题
	private byte[] body;//消息内容
	private Long timestamp;//时间戳
	
	private static final String default_topic = "topic";
	
	public String getTopic() {
		if(topic == null){
			topic = default_topic;
		}
		return topic;
	}
	public void setTopic(String topic) {
		
		this.topic = topic;
	}
	public String getMessageID() {
		return messageID;
	}
	public void setMessageID(String messageID) {
		this.messageID = messageID;
	}
	public byte[] getBody() {
		return body;
	}
	public void setBody(byte[] body) {
		this.body = body;
	}
	 
	public Long getTimestamp() {
		return timestamp;
	}
	public void setTimestamp(Long timestamp) {
		this.timestamp = timestamp;
	}
	public MessageSourceEnum getSource() {
		return source;
	}
	public void setSource(MessageSourceEnum source) {
		this.source = source;
	}
	public MessageSourceEnum getTarget() {
		return target;
	}
	public void setTarget(MessageSourceEnum target) {
		this.target = target;
	} 
	
	@Override
	public String toString(){	
		String bo = "";
		try {
			if(body != null){
				bo = new String(body,"UTF-8");
			}
			
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		return String.format(" topic:%s  ID:%s  source:%s  target:%s bo:%s ",
				topic,
				messageID,
				source,
				target,
				bo);
	}
	
}
